'''
* This is the projet for Brtc LlmOps Platform
* @Author Leon-liao <liaosiliang@alltman.com>
* @Description //TODO 
* @File: config.py
* @Time: 2025/11/5
* @All Rights Reserve By Brtc
'''
import os
from typing import Any

from config.default_config import DEFAULT_CONFIG


def _get_env(key:str)->Any:
    """从环境变量中获取配置项, 如果找不到则从默认配置中查找"""
    return os.getenv(key, DEFAULT_CONFIG.get(key))

def _get_bool_env(key:str)->bool:
    """从环境变量中获取布尔值类型的配置项,如果找不到返回默认值"""
    value:str = _get_env(key)
    return value.lower() == "true" if value is not None else False


class Config:

    def __init__(self):
        #从环境变量中获取WTF 配置
        self.WTF_CSRF_ENABLED = _get_bool_env("WTF_CSRF_ENABLED")
        #从环境变量钟获取postgres配置
        self.SQLALCHEMY_DATABASE_URI =_get_env("SQLALCHEMY_DATABASE_URI")
        self.SQLALCHEMY_ENGINE_OPTIONS={
            "pool_recycle":  _get_env("SQLALCHEMY_POOL_RECYCLE"),
            "pool_size": _get_env("SQLALCHEMY_POOL_SIZE"),
        }
        self.SQLALCHEMY_ECHO = _get_bool_env("SQLALCHEMY_ECHO")

        # redis 的 配置
        self.REDIS_HOST = _get_env("REDIS_HOST")
        self.REDIS_PORT = _get_env("REDIS_PORT")
        self.REDIS_USERNAME = _get_env("REDIS_USERNAME")
        self.REDIS_PASSWORD = _get_env("REDIS_PASSWORD")
        self.REDIS_DB = _get_env("REDIS_DB")
        self.REDIS_USE_SSL = _get_bool_env("REDIS_USE_SSL")

        # celery 异步任务配置
        self.CELERY = {
            "broker_url": f"redis://{self.REDIS_HOST}:{self.REDIS_PORT}/{int(_get_env('CELERY_BROKER_DB'))}",
            "result_backend": f"redis://{self.REDIS_HOST}:{self.REDIS_PORT}/{int(_get_env('CELERY_RESULT_BACKEND_DB'))}",
            "task_ignore_result": _get_bool_env("CELERY_TASK_IGNORE_RESULT"),
            "result_expires": int(_get_env("CELERY_RESULT_EXPIRES")),
            "broker_connection_retry_on_startup": _get_bool_env("CELERY_BROKER_CONNECTION_RETRY_ON_STARTUP"),
        }

        #weaviate 向量数据库配置
        self.WEAVIATE_HTTP_HOST = _get_env("WEAVIATE_HTTP_HOST")
        self.WEAVIATE_HTTP_PORT = _get_env("WEAVIATE_HTTP_PORT")
        self.WEAVIATE_GRPC_HOST = _get_env("WEAVIATE_GRPC_HOST")
        self.WEAVIATE_GRPC_PORT = _get_env("WEAVIATE_GRPC_PORT")
